package com.erpanderp.wam.cm.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.RoleDAO;
import com.erpanderp.wam.adm.model.service.RolePermissionDAO;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.cm.model.entity.EmpPersonalResume;
import com.erpanderp.wam.cm.model.service.UploadResumeDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean
public class ResumesBean implements Serializable{
	
	private static final long serialVersionUID = 1L;	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	
    private List<SAPERPModule> saperpModules=new ArrayList<SAPERPModule>();
    private Map<String, String> modulesMap = new HashMap<String, String>();
    private List<EmpPersonalResume> employees = new ArrayList<EmpPersonalResume>();
    private List<EmpPersonalResume> employeesListTobeRendered = new ArrayList<EmpPersonalResume>(); 
    
    private SAPERPModuleDAO saperpModuleDAO=new SAPERPModuleDAO();
    private UploadResumeDAO uploadResumeDAO = new UploadResumeDAO();
    
    private EmpPersonalResume employeeResume = new EmpPersonalResume();
    
    private boolean renderEmployeeList;
    private String selectedModule ;
	private String location;
	private String employeeCode;
	private String employeeName;
	private boolean renderNoResult;
    private StreamedContent file; 
    
	public ResumesBean() {				
		saperpModules = saperpModuleDAO.getSAPERPModules();
		for(SAPERPModule saperpModule : saperpModules){
			modulesMap.put(saperpModule.getDisplayName(), saperpModule.getDisplayName());
		}	
	}

	//SEARCH ACTION
	public String searchEmployees(){
		System.out.println("Search Action...");
			
		employees = uploadResumeDAO.getAllResumes(selectedModule, location, employeeCode, employeeName);
		
		if(employees != null && employees.size() > 0){
			setRenderEmployeeList(true);
			renderNoResult=false;
		}else{
			setRenderEmployeeList(false);
			renderNoResult=true;
		}		
		return null;
	}
	
	//FILE DOWNLOAD ACTION
	public void fileDownload(ActionEvent e) throws FileNotFoundException{
		Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Long employeeId = Long.parseLong(params.get("employeeId"));
		System.out.println("EMPID FROM PARAM = "+employeeId);
		employeeResume = uploadResumeDAO.getEmployeeResumeByEmployeeId(employeeId);		
		ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();        
	    File result = new File(extContext.getRealPath("//employeePersonalResumes//"+employeeResume.getFilePath()));        
	    InputStream stream = new FileInputStream(result.getAbsolutePath());
	    file = new DefaultStreamedContent(stream, "application/msword",employeeResume.getFilename());		
	}
	
	
	public void deleteClickActionListener(ActionEvent e){
		System.out.println("DELETE>..");
	
	}
	public List<EmpPersonalResume> getEmployees() {
		return employees;
	}

	public void setEmployees(List<EmpPersonalResume> employees) {
		this.employees = employees;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(String selectedModule) {
		this.selectedModule = selectedModule;
	}

	public boolean isRenderEmployeeList() {
		return renderEmployeeList;
	}

	public void setRenderEmployeeList(boolean renderEmployeeList) {
		this.renderEmployeeList = renderEmployeeList;
	}
	
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public StreamedContent getFile() {
		return file;
	}

	public void setFile(StreamedContent file) {
		this.file = file;
	}

	public EmpPersonalResume getEmployeeResume() {
		return employeeResume;
	}

	public void setEmployeeResume(EmpPersonalResume employeeResume) {
		this.employeeResume = employeeResume;
	}

	public List<EmpPersonalResume> getEmployeesListTobeRendered() {
		return employeesListTobeRendered;
	}

	public void setEmployeesListTobeRendered(
			List<EmpPersonalResume> employeesListTobeRendered) {
		this.employeesListTobeRendered = employeesListTobeRendered;
	}

	public List<SAPERPModule> getSaperpModules() {
		return saperpModules;
	}

	public void setSaperpModules(List<SAPERPModule> saperpModules) {
		this.saperpModules = saperpModules;
	}

	public boolean isRenderNoResult() {
		return renderNoResult;
	}

	public void setRenderNoResult(boolean renderNoResult) {
		this.renderNoResult = renderNoResult;
	}

}
